package com.liang.demo.context;

import com.liang.demo.config.security.AdminUserDetails;
import com.liang.demo.po.Admin;
import com.liang.demo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * @author 陈琪亮
 * @version 1.0
 * @date 2021/3/26 23:29
 */
@Component
public class WebContext {

    private final AdminService adminService;

    @Autowired
    public WebContext(AdminService adminService) {
        this.adminService = adminService;
    }

    public Admin getCurrentUser() {
        AdminUserDetails springUser = (AdminUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Admin admin = adminService.getAdminByUsername(springUser.getUsername());
        if (null == springUser) {
            return null;
        }
        return admin;
    }

}
